package com.example.xdemo.slice;

import com.example.xdemo.MyAdapter;
import com.example.xdemo.ResourceTable;
import com.jcodecraeer.xrecyclerview.ProgressStyle;
import com.jcodecraeer.xrecyclerview.StickyScrollLinearLayout;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.window.dialog.ToastDialog;
import ohos.eventhandler.EventHandler;
import ohos.eventhandler.EventRunner;

import java.util.ArrayList;

public class LinearStickyScrollAbilitySlice extends AbilitySlice {
    private XRecyclerView mXRecyclerView;
    private MyAdapter<String> mAdapter;
    private ArrayList<String> listData;
    private int times = 0;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_linear_sticky_scroll);
        initXR();
        final Component topView = findComponentById(ResourceTable.Id_abt_linear_sticky_scroll_topview);
        final Component tabView = findComponentById(ResourceTable.Id_abt_linear_sticky_scroll_tabview);
        final Component content = findComponentById(ResourceTable.Id_abt_linear_sticky_scroll_content);
        final StickyScrollLinearLayout sLayout = (StickyScrollLinearLayout)findComponentById(
                ResourceTable.Id_abt_linear_sticky_scroll);
        sLayout.setLayoutRefreshedListener(new Component.LayoutRefreshedListener() {
            @Override
            public void onRefreshed(Component component) {
                sLayout.setInitInterface(new StickyScrollLinearLayout.StickyScrollInitInterface() {
                    @Override
                    public Component setTopView() {
                        return topView;
                    }

                    @Override
                    public Component setTabView() {
                        return tabView;
                    }

                    @Override
                    public Component setContentView() {
                        return content;
                    }
                });
            }
        });
    }


    private void initXR(){
        mXRecyclerView = (XRecyclerView)findComponentById(ResourceTable.Id_abt_linear_sticky_scroll_xrecyclerview);
        mXRecyclerView.setPullRefreshEnabled(false);
        mXRecyclerView.setLoadingMoreProgressStyle(ProgressStyle.BallClipRotate);

        mXRecyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {

            }

            @Override
            public void onLoadMore() {

                if(times < 2){
                    new EventHandler(EventRunner.getMainEventRunner()).postTask(new Runnable() {
                        @Override
                        public void run() {
                            for(int i = 0; i < 10 ;i++){
                                listData.add("item" + (1 + listData.size() ) );
                            }
                            mXRecyclerView.loadMoreComplete();
                            mAdapter.notifyDataChanged();
                        }
                    },1000);
                }else {
                    new EventHandler(EventRunner.getMainEventRunner()).postTask(new Runnable() {
                        @Override
                        public void run() {
                            for(int i = 0; i < 10 ;i++){
                                listData.add("item" + (1 + listData.size() ) );
                            }
                            mXRecyclerView.setNoMore(true);
                            mAdapter.notifyDataChanged();
                        }
                    },1000);
                }
                times++;
            }
        });

        listData = new  ArrayList<String>();
        for(int i=0;i<20;i++){
            listData.add(" data -- "+i);
        }

        mAdapter = new MyAdapter<>(getContext(),listData,ResourceTable.Layout_list_item);

        mAdapter.setOnItemClickListener(new XRecyclerView.RecyclerAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(Component component, int position) {

                new ToastDialog(getContext()).setText("nihao"+position).show();
            }
        });
        mXRecyclerView.setItemProvider(mAdapter);
    }
}
